package com.neudu.glsb.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neudu.glsb.pojo.Member;
import com.neudu.glsb.service.MemberService;
import com.neudu.glsb.mapper.MemberMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
* @author Lenovo
* @description 针对表【member】的数据库操作Service实现
* @createDate 2025-10-20 10:23:17
*/
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member>
        implements MemberService{

    // 充值赠送比例: 充100送10
    private static final BigDecimal GIFT_RATIO = new BigDecimal("0.1");
    // 积分兑换比例: 1积分=0.1元
    private static final BigDecimal EXCHANGE_RATIO = new BigDecimal("0.1");

    @Override
    @Transactional
    public boolean recharge(Integer id, BigDecimal amount) {
        if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            return false;
        }
        // 计算赠送金额（仅用于余额）
        BigDecimal giftAmount = amount.multiply(GIFT_RATIO);
        // 总到账金额（本金+赠送）
        BigDecimal totalAmount = amount.add(giftAmount);

        // 正确：只传递 id 和 totalAmount，无 points 参数
        return baseMapper.recharge(id, totalAmount) > 0;
    }

    @Override
    @Transactional
    public boolean exchangePoints(Integer id, Integer points) { // id为Integer类型
        if (points == null || points <= 0) {
            return false;
        }
        // 计算兑换的金额
        BigDecimal exchangeAmount = new BigDecimal(points).multiply(EXCHANGE_RATIO);
        return baseMapper.exchangePoints(id, points, exchangeAmount) > 0; // 传入Integer类型id
    }

    // 新增：增加会员积分
    @Override
    public boolean addPoints(Integer memberId, Integer points) {
        LambdaUpdateWrapper<Member> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Member::getId, memberId)
                .setSql("points = points + " + points + ", updated_time = CURRENT_TIMESTAMP");
        return this.update(updateWrapper);
    }
    // 退会员实现（新增）
    @Override
    public boolean cancelMember(Integer id) {
        // 调用mapper的cancelMember方法
        int rows = baseMapper.cancelMember(id);
        return rows > 0; // 影响行数>0表示退卡成功
    }
}




